/*
    $Header: /Windows/Shell/ShellExt.root/ShellExt/src/CQurInfo.cpp 1     08-01-05 0:08 Ken $
    $Log: /Windows/Shell/ShellExt.root/ShellExt/src/CQurInfo.cpp $
 * 
 * 1     08-01-05 0:08 Ken
*/

// ..\Src\CQueryInfo.cpp : Implementation of CQueryInfo

#include "stdafx.h"
#include "..\Src\CQurInfo.h"
#include "misc.h"

// CQurInfo

BOOL CQurInfo::VSrcInit(UINT uSrcId, TCHAR* pszFl, BOOL fLoad /*called by Load*/)
{
    if(FALSE!=m_cjunct.Initialize(pszFl))
    {
        m_fJunct=TRUE;
        return TRUE;
    }

    HANDLE hFl=CreateFile( pszFl, 0, FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_READ,
                                        NULL, OPEN_EXISTING, 0, 0 );
    if(INVALID_HANDLE_VALUE!=hFl)
    {
        if( (FALSE!=GetFileInformationByHandle(hFl,&m_flinfo))
                &&(1<m_flinfo.nNumberOfLinks) )    // hard link
        {
            m_fJunct=FALSE;
            CloseHandle(hFl);
            return TRUE;
        }
        CloseHandle(hFl);
    }

    return FALSE;
}


static const WCHAR WSYMBOLIC_LINK[]={L"SYMBOLIC_LINK"};
static const WCHAR WJUNCTION[]={L"JUNCTION"};
static const WCHAR WMOUNT_POINT[]={L"\r\nMOUNT_POINT"};

static const WCHAR WNO_TARGET[]={L"Target not exist!"};
// JunctionType Src:...; Tgt:...
HRESULT CQurInfo::GetInfoTip ( DWORD dwFlags, LPWSTR* ppwszTip )
{
    LPMALLOC pMalloc;
    if ( FAILED( SHGetMalloc ( &pMalloc )))
    {
        return E_FAIL;
    }

    DWORD dwTipLen;
    if(FALSE!=m_fJunct)
    {
        const WCHAR* pwJunctType;
        switch(m_cjunct.GetJunctionType())
        {
        case CJunction::SYMBOLIC_LINK:
            pwJunctType=WSYMBOLIC_LINK;
            break;

        case CJunction::JUNCTION:
            pwJunctType=WJUNCTION;
            break;

        case CJunction::MOUNT_POINT:
            pwJunctType=WMOUNT_POINT;
            break;

        default:
            ATLTRACE(_T("Invalid juction type! why here!!!\r\n"));
            return E_FAIL;
        }

        const WCHAR* pwszNoTgt=NULL;
        dwTipLen=_tcslen(_T(" Name:; Tgt:."))+1;
        dwTipLen+=wcslen(pwJunctType);
        dwTipLen+=_tcslen(m_cjunct.GetJunctionName());
        dwTipLen+=_tcslen(m_cjunct.GetJunctionTgt());
        if( FALSE==PathFileExists(m_cjunct.GetJunctionTgt()) )
        {
            pwszNoTgt=WNO_TARGET;
            dwTipLen+=ARRAYOF(WNO_TARGET);
        }

        *ppwszTip=(LPWSTR)pMalloc->Alloc( dwTipLen*sizeof(wchar_t));
        if (NULL == *ppwszTip)
        {
            pMalloc->Release();
            return E_OUTOFMEMORY;
        }

        USES_CONVERSION;     //some convert var defines
        swprintf( *ppwszTip, L"%s Name:%s; Tgt:%s.", pwJunctType,
        T2W(m_cjunct.GetJunctionName()), T2W(m_cjunct.GetJunctionTgt()));

        if(NULL!=pwszNoTgt)
        {
            wcscat(*ppwszTip, pwszNoTgt);
        }
    }
    else
    {
        dwTipLen=_tcslen(_T("HdLnkNum:DDDD, Id(H):0xXXXXXXXX, Id(L):0xXXXXXXXX."));
        *ppwszTip=(LPWSTR)pMalloc->Alloc( dwTipLen*sizeof(wchar_t));
        if (NULL == *ppwszTip)
        {
            pMalloc->Release();
            return E_OUTOFMEMORY;
        }

        swprintf( *ppwszTip, L"HdLnkNum:%d, Id(H):0x%x, Id(L):0x%x.", m_flinfo.nNumberOfLinks,
        m_flinfo.nFileIndexHigh, m_flinfo.nFileIndexHigh);
    }

    pMalloc->Release();
    return S_OK;

}
